<template>
  <div class="login-title">
    <img class="icon" src="@/assets/image/logo.png" alt="logo" />
    <h2 class="title">Vue-Admin-Perfect</h2>
  </div>
  <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
    <el-form-item label="" prop="username">
      <el-input
        v-model="ruleForm.username"
        placeholder="请输入用户名"
        auto-complete="on"
        style="position: relative"
        @keyup.enter="submitForm(ruleFormRef)"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><UserFilled /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item label="" prop="password">
      <el-input
        v-model="ruleForm.password"
        placeholder="请输入密码"
        auto-complete="on"
        :type="passwordType"
        @keyup.enter="submitForm(ruleFormRef)"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><GoodsFilled /></el-icon>
        </template>
        <template #suffix>
          <div class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </div>
        </template>
      </el-input>
    </el-form-item>

    <!-- 验证码 -->
    <el-form-item prop="captchaCode">
      <el-input
        v-model="ruleForm.captchaCode"
        auto-complete="off"
        placeholder="请输入验证码"
        class="w-[60%]"
        @keyup.enter="submitForm(ruleFormRef)"
      >
        <template #prefix>
          <svg-icon icon-class="captcha" />
        </template>
        <template #suffix>
          <div class="captcha">
            <el-image :src="captchaBase64" class="w-[120px] h-[48px] cursor-pointer" @click="getCaptcha">
              <template #error>
                <div class="image-slot">
                  <i-ep-picture />
                </div>
              </template>
            </el-image>
          </div>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item style="width: 100%">
      <el-button :loading="loading" class="login-btn" type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
  import { ref, reactive, onMounted } from 'vue'
  import type { FormInstance } from 'element-plus'
  import { ElNotification } from 'element-plus'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/store/modules/user'
  import { getTimeStateStr } from '@/utils/index'
  import { getCaptchaApi } from '@/api/auth'

  const router = useRouter()
  const UserStore = useUserStore()
  const ruleFormRef = ref<FormInstance>()
  const captchaBase64 = ref() // 验证码图片Base64字符串
  const passwordType = ref('password')
  const loading = ref(false)

  const rules = reactive({
    password: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    username: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  })

  // 表单数据
  const ruleForm = reactive({
    username: 'admin',
    password: '123456',
    captchaCode: '',
    captchaKey: '',
  })

  onMounted(() => {
    getCaptcha()
  })

  /**
   * 获取验证码
   */
  function getCaptcha() {
    getCaptchaApi().then(({ data }) => {
      ruleForm.captchaKey = data.captchaKey
      captchaBase64.value = data.captchaBase64
    })
  }
  // 显示密码图标
  const showPwd = () => {
    passwordType.value = passwordType.value === 'password' ? '' : 'password'
  }

  const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        loading.value = true
        // 登录
        setTimeout(async () => {
          try {
            await UserStore.login(ruleForm)
            await router.push({
              path: '/',
            })
            ElNotification({
              title: getTimeStateStr(),
              message: '欢迎登录 Vue Admin Perfect',
              type: 'success',
              duration: 3000,
            })
          } finally {
            loading.value = false
          }
        }, 1000)
      } else {
        console.log('error submit!')
        return false
      }
    })
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';
</style>
